package com.chow.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class GetIMMessage {
	public static void main(String[] args) {
		String s = new GetIMMessage()
				.getLastMsg("E:\\workspace\\aries\\UI\\testim.exe");
		System.out.println(s);
		// String str = new
		// com.chow.test.GetIMMessage().getLastMsg("E:\\workspace\\aries\\UI\\testim.exe");
		// System.out.println(str);
		// String TADir = "E:\\workspace\\aries\\UI\\";
		// String cmd = TADir + "testim.exe";
		// Process p = null;
		// try {
		// p = Runtime.getRuntime().exec(cmd);
		// } catch (IOException e) {
		// }
		//
		// InputStream is = p.getInputStream();
		// InputStreamReader isr = new InputStreamReader(is);
		// BufferedReader br = new BufferedReader(isr);
		// String line = null;
		// String html = "";
		// try {
		// while ((line = br.readLine()) != null) {
		// html += line;
		// }
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		//
		// // System.out.println(html);
		//
		// Document doc = Jsoup.parse(html);
		// Elements tds = doc.select("td.msgText");
		// // Element lastMsg = null;
		// // if (fonts.size() > 0)
		// // lastMsg = fonts.last();
		// // if (lastMsg == null) {
		// // System.out.println("last msg is null");
		// // }
		// for (Element td : tds) {
		// System.out.println(td.child(0).ownText());
		// // if (td.childNodes() != null && td.childNodes().size() > 0)
		// // System.out.println(td.childNodes().get(0).toString());
		// }
	}

	public String getLastMsg(String cmd) {
		// String TADir = "E:\\workspace\\aries\\UI\\";
		// String cmd = TADir + "testim.exe";
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
		}

		InputStream is = p.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		String html = "";
		try {
			while ((line = br.readLine()) != null) {
				html += line;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		Document doc = Jsoup.parse(html);
		Elements tds = doc.select("td.msgText");
		if (tds.size() > 0)
			return tds.last().child(0).ownText();
		return "";
	}
}
